﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Nodes;
using System.Threading.Tasks;

namespace bigscreen_ai_demo.DB
{
    public class DBContext
    {
        //注意：不能写成静态的
        public SqlSugarClient Db;//用来处理事务多表查询和复杂的操作

        public DBContext(ICacheService cacheService = null)
        {
            Db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = Config.DBConnectionStr,
                DbType = DbType.MySql,
                InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
                IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样
                ConfigureExternalServices = new ConfigureExternalServices()
                {
                    DataInfoCacheService = cacheService  //配置我们创建的缓存类，具体用法看标题5
                }

            });
            //调式代码 用来打印SQL 
            Db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(sql + "\r\n" +
                    Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                Console.WriteLine();
            };

        }


        public void CodeFirstSync()
        {
            Db.DbMaintenance.CreateDatabase();
            Db.CodeFirst.SetStringDefaultLength(200)
                .InitTables(typeof(ModelA),typeof(ModelA_OUTPUT));

        }

    }
}
